package com.hebust.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hebust.entity.Students;
import com.hebust.mapper.StudentMapper;
import com.hebust.mapper.StudentsMapper;
import com.hebust.service.StudentsService;
import com.hebust.utils.ResultVo;
import com.hebust.utils.admin.AdminDepartment;
import com.hebust.utils.student.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class StudentsServiceImpl implements StudentsService {

    @Autowired
    private StudentsMapper studentsMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public ResultVo login(String username, String password) {
        Students s = new Students();
        s.setSNumber(username);
        Students students = studentsMapper.selectOne(s);
        if (students == null) {
            return new ResultVo(1000, "用户名不存在", null);
        } else {
            if (students.getSPassword().equals(password)) {
                return new ResultVo(1002, "登录成功", students);
            } else {
                return new ResultVo(1001, "密码错误", null);
            }
        }
    }

    @Override
    public ResultVo show_selectcourse(String name, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<SelectCourse> selectCourses = studentMapper.show_selectcourse(name);
        PageInfo page = new PageInfo(selectCourses);
        if (selectCourses.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo add_selectcourse(int sid, int cid, String name, int tid) {
        int i = studentMapper.add_selectcourse(sid, cid, name, tid);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo update_select(int id) {
        int i = studentMapper.update_select(id);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo select_course(int id, int sid) {
        List<SelectResult> selectResults = studentMapper.select_course(id, sid);
        if (selectResults.size() > 0) {
            return new ResultVo(1002, "成功", selectResults);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo insert_teacherscore(int sid, int tid, int cid) {
        int i = studentMapper.insert_teacherscore(sid, tid, cid);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo select_grade(int sid) {
        int i = studentMapper.select_grade(sid);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo show_studentcourse(int sid, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<StudentCourse> studentCourses = studentMapper.show_studentcourse(sid);
        PageInfo page = new PageInfo(studentCourses);
        if (studentCourses.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo search_id(int sid, String name) {
        StudentCourseResult studentCourseResult = studentMapper.search_id(sid, name);
        if (studentCourseResult != null) {
            return new ResultVo(1002, "成功", studentCourseResult);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo delete_studentcourse(int id) {
        int i = studentMapper.delete_studentcourse(id);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo update_studentcourse(int cid) {
        int i = studentMapper.update_studentcourse(cid);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo delete_teacherscore(int sid, int cid) {
        int i = studentMapper.delete_teacherscore(sid, cid);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo show_exam(int sid, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<StudentExam> studentExamList = studentMapper.show_exam(sid);
        PageInfo page = new PageInfo(studentExamList);
        if (studentExamList.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);

    }

    @Override
    public ResultVo show_teacherscore(int sid, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<TeacherScore> teacherScore = studentMapper.show_teacherscore(sid);
        PageInfo page = new PageInfo(teacherScore);
        if (teacherScore.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo update_teacherscore(int id, int score) {
        int i = studentMapper.update_teacherscore(id, score);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo update_score(int sid, int cid, int score) {
        int i = studentMapper.update_score(sid, cid, score);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo show_score(int sid, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<ScoreSearch> scoreSearchList = studentMapper.show_score(sid);
        PageInfo page = new PageInfo(scoreSearchList);
        if (scoreSearchList.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo update_studentpassword(int sid, String password) {
        int i = studentMapper.update_studentpassword(sid, password);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo show_courseschedule(int sid) {
        List<Course> courseList = studentMapper.show_courseschedule(sid);

        List<JSONObject> list = new ArrayList<>();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        JSONObject jsonObject3 = new JSONObject();
        JSONObject jsonObject4 = new JSONObject();
        jsonObject1.put("section", "第一大节");
        jsonObject1.put("time", "08:00-09:40");
        jsonObject2.put("section", "第二大节");
        jsonObject2.put("time", "10:00-11:40");
        jsonObject3.put("section", "第三大节");
        jsonObject3.put("time", "13:40-15:20");
        jsonObject4.put("section", "第四大节");
        jsonObject4.put("time", "15:40-17:20");
        list.add(jsonObject1);
        list.add(jsonObject2);
        list.add(jsonObject3);
        list.add(jsonObject4);

        courseList.forEach(res -> {
            CourseList courseList1 = new CourseList();
            courseList1.setName(res.getCname());
            courseList1.setLocation(res.getLocation());
            String select = res.getTtime().split(";")[0].split(" ")[1];
            switch (select) {
                case "第一大节":
                    jsonObject1.put(res.getTtime().split(";")[0].split(" ")[0], courseList1);
                    break;
                case "第二大节":
                    jsonObject2.put(res.getTtime().split(";")[0].split(" ")[0], courseList1);
                    break;
                case "第三大节":
                    jsonObject3.put(res.getTtime().split(";")[0].split(" ")[0], courseList1);
                    break;
                case "第四大节":
                    jsonObject4.put(res.getTtime().split(";")[0].split(" ")[0], courseList1);
                    break;
            }
            String select1 = res.getTtime().split(";")[1].split(" ")[1];
            switch (select1) {
                case "第一大节":
                    jsonObject1.put(res.getTtime().split(";")[1].split(" ")[0], courseList1);
                    break;
                case "第二大节":
                    jsonObject2.put(res.getTtime().split(";")[1].split(" ")[0], courseList1);
                    break;
                case "第三大节":
                    jsonObject3.put(res.getTtime().split(";")[1].split(" ")[0], courseList1);
                    break;
                case "第四大节":
                    jsonObject4.put(res.getTtime().split(";")[1].split(" ")[0], courseList1);
                    break;
            }
        });

        return new ResultVo(1002, "成功", list);
    }
}
